"""
    Copyright (C) 2010 Sebastiano Merlino

    This file is part of pystful.

    pystful is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    pystful is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pystful.  If not, see <http://www.gnu.org/licenses/>.

"""

__license__ = 'GPL http://www.gnu.org/licenses/gpl.txt'
__author__ = "Sebastiano Merlino <etr@pensieroartificiale.com>"
__version__ = '0.1'

import sys
import pystful
from pystful import Connection

__doc__ = """
Console documentation
"""

class Console:
    Connections = {}

    def createCommand(self, command_list):
        try:
            redirect_index = command_list.index(">")
        except:
            print "Connection need to have a name defined by redirection"
            return
        if len(command_list) < redirect_index+2:
            print "Connection need to have a name defined by redirection"
            return
        pre_command = command_list[:redirect_index]
        if len(pre_command) == 1:
            print "Nothing to connect to"
            return
        url = ""
        user = None
        password = None
        if len(pre_command) >= 2:
            url = pre_command[1]
            if len(pre_command) >= 3:
                user = pre_command[2]
                if len(pre_command) >= 4:
                    password = pre_command[3]
        self.Connections[command_list[redirect_index+1]] = Connection(url, user, password) 
        print "Created connection to "+url+" named "+command_list[redirect_index+1]

    def getCommand(self, command_list):
        conn = None
        if ">" in command_list:
            redirect_index = command_list.index(">")
            if len(command_list) < redirect_index+2:
                print "You have redirected to nothing"
                return
            else:
                try:
                    conn = self.Connections[redirect_index+1]
                except:
                    print "This connection does not exists"
                    return
            pre_command = command_list[:redirect_index]
        else:
            pre_command = command_list
        if len(pre_command) == 1:
            print "Nothing to work on"
            return
        url = ""
        args = None
        headers = None
        if len(pre_command) >= 2:
            url = pre_command[1]
            if len(pre_command) >= 3:
                args = pre_command[2]
                if len(pre_command) >= 4:
                    headers = pre_command[3]
        if conn != None:
            pass 
        else:
            pass
#### CONTINUE TO IMPLEMENT
            

    def workOnCommand(self, command):
        command_list = command.split()
        if len(command_list) <= 0:
            return
        if command_list[0] == "help":
            print __doc__
        elif command_list[0] == "quit" or command_list[0] == "exit":
            return
        elif command_list[0] == "create":
            self.createCommand(command_list)
        elif command_list[0] == "show":
            print str(self.Connections.keys())
        elif command_list[0] == "get":
            self.getCommand(command_list)
        elif command_list[0] == "head":
            self.headCommand(command_list)
        elif command_list[0] == "delete":
            self.deleteCommand(command_list)
        elif command_list[0] == "post":
            self.postCommand(command_list)
        elif command_list[0] == "put":
            self.putCommand(command_list)
        elif command_list[0] == "remove":
            self.removeCommand(command_list)
        else:
            print "Unknown command!"
        return

    def main(self):
        try:
            command = ""
            while command != "quit" and command != "exit":
                print "pystful>>",
                command = raw_input()
                self.workOnCommand(command)
        except Exception as err:
            print err
            sys.exit(-1)
